package top.wilsonlv.jaguar.oauth2.resourceserver.api;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.wilsonlv.jaguar.commons.web.response.JsonResult;
import top.wilsonlv.jaguar.oauth2.resourceserver.config.ResourceServerConfigurer;
import top.wilsonlv.jaguar.oauth2.resourceserver.properties.JaguarResourceServerProperties;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lvws
 * @since 2022/3/31 0031
 */
@RestController
@RequiredArgsConstructor
public class OAuthResourceServiceApi {

    public static final String PATH_RESOURCE_SERVER_SCOPES = "/feign/resourceServer/scopes";

    private final ResourceServerConfigurer resourceServerConfigurer;

    private final JaguarResourceServerProperties jaguarResourceServerProperties;


    @GetMapping(PATH_RESOURCE_SERVER_SCOPES)
    public JsonResult<String[]> scopes() {
        String[] scopes;
        Map<String, String> scopeUrls = jaguarResourceServerProperties.getScopeUrls();
        if (scopeUrls != null) {
            List<String> values = new ArrayList<>(scopeUrls.values());
            values.add(resourceServerConfigurer.getOtherScopeName());
            values.add(resourceServerConfigurer.getAllScopeName());
            scopes = values.toArray(new String[0]);
        } else {
            scopes = new String[]{resourceServerConfigurer.getAllScopeName()};
        }
        return JsonResult.success(scopes);
    }

}
